package com.agentlab.imitation.web.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.agentlab.imitation.bean.User;
import com.agentlab.imitation.common.CommonConstant;
import com.agentlab.imitation.service.UserService;
import com.agentlab.imitation.service.admin.AdminService;
import com.agentlab.imitation.util.DateTimeUtils;
import com.agentlab.imitation.util.ToString;
import com.agentlab.imitation.util.WebUtil;


@Controller 
public class UserLogonAction {

	@Resource UserService usersService;
	String loginbz = "";
	
	private String goingToURL;
	private String message;
	
	//访问首页
	public String myHomepage(){
		return "homepage";
	}
	
	public String userLogonUI(){
		if(CommonConstant.LOGIN_BZ.equals(getLoginbz())){
			return "success";
		}else{
				
			return "logonui";
		}
	}
	/**
	 * 用户登录
	 */
	public String userLogon(){
		if(CommonConstant.LOGIN_BZ.equals(getLoginbz())){
			return "success";
		}
		HttpServletRequest reg = ServletActionContext.getRequest();
		  HttpSession session = reg.getSession();
		  //request_locale=zh_CN reg.getLocale();
		  reg.setAttribute("request_locale", "en_US");
		  //System.out.println("头文件信息语言："+reg.getHeader("accept-language"));
		  //System.out.println("Locale："+reg.getLocale());
		  String locale = reg.getHeader("accept-language");
		 // System.out.println("---WW_TRANS_I18N_LOCALE:"+((ActionContext) session).get("WW_TRANS_I18N_LOCALE"));
		if(StringUtils.isNotEmpty(locale)){
		  if(!locale.equals("en-US") && !locale.equals("zh-CN")){
			session.setAttribute("WW_TRANS_I18N_LOCALE", "en_US");
		  }
		}
		
		//判断验证码
		String captcha = reg.getParameter("checkcode").toUpperCase().trim();
		if(!captcha.equals(reg.getSession().getAttribute(CommonConstant.RANDOM_SESSION).toString().toUpperCase())){
			reg.setAttribute("message", "验证码输入错误");
			return "logonui";
		}
		
		if(StringUtils.isNotBlank(reg.getParameter("loginusername")) 
				&& StringUtils.isNotBlank(reg.getParameter("loinpassword")) ){
			User user = usersService.checkUser(reg.getParameter("loginusername").trim(),
					reg.getParameter("loinpassword").trim());
			System.out.println("-----user-:"+ new ToString(user).toString());
			
			if( user !=null){  //登录成功
				if(user.getStatus() ==1){ //已经激活
					reg.getSession().setAttribute(CommonConstant.USER_SESSION, user);
					setLoginbz(CommonConstant.LOGIN_BZ);
					user.setLastlogintime(DateTimeUtils.getDateTimeStamp());
					user.setLastloginip(WebUtil.getIpAddr(reg));
					usersService.update(user);
					return "success";
				}//未激活用户
				else{
					reg.setAttribute("message", "用户没有激活");
					return "logonui";
				}
				/*
				String tourl = (String) session.getAttribute(CommonConstant.GOING_TO_URL_KEY);
				
				System.out.println("-----userid-:"+userid+"tourl:"+tourl);
				System.out.println("-----tourl-:"+tourl);
			
				if (StringUtils.isNotBlank(tourl)){
				//if (getGoingToURL() != null){
					setGoingToURL(tourl);
					session.removeAttribute(CommonConstant.GOING_TO_URL_KEY);
					//return "toredirect";
				}else{
					setGoingToURL(reg.getContextPath());
				}
				*/
			}else  //登录失败
			{
				reg.setAttribute("message", "用户名或密码错误");
				return "logonui";
			}
		}
		
		return "logonui"; 
	}
	
	public String getLoginbz() {
		if(ServletActionContext.getRequest().getSession().getAttribute(CommonConstant.USER_SESSION) != null){
			return CommonConstant.LOGIN_BZ;
		}else{
			return "";
		}
	}
	public void setLoginbz(String loginbz) {
		this.loginbz = loginbz;
	}

	public String getGoingToURL() {
		return goingToURL;
	}

	public void setGoingToURL(String goingToURL) {
		this.goingToURL = goingToURL;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}
